﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DBTool
{
    using PLAN_TABLE_NAME = System.String; 
    using SELECTED_PATH_FOLDER = System.String;
    using SELECTED_FILE = System.String;

    class SvnManager
    {
        #region Member Variables
        private const string _log = "CsvCommit_ScriptParsingTool";
        private const string _settingIniFileName = "SvnSetting.ini";
        private const string _parsingFolder = "Parse";
        private const string _reverseParsingFolder = "ReverseParse";

        private SVNInfo _svnInfo = null;
        private string _fileMoveAndCommitPath = string.Empty;
        #endregion


        #region Ctor
        public SvnManager()
        {
            IniFile ini = new IniFile( System.Windows.Forms.Application.StartupPath + "/" + _settingIniFileName );

            string svnPath = ini.ReadValue("SVN", "PATH");
            if ( true == string.IsNullOrEmpty( svnPath ) )
            {
                MessageBox.Show("SVN Path Is NULL. Check Ini File", "SVN Path Is Null", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            _svnInfo = new SVNInfo( svnPath, null, null );

            _fileMoveAndCommitPath = ini.ReadValue("FILE_MOVE_COMMIT_PATH", "PATH");

            if ( true == string.IsNullOrEmpty( _fileMoveAndCommitPath ) )
            {
                MessageBox.Show("FILE_MOVE_COMMIT_PATH Is NULL. Check Ini File", "FILE_MOVE_COMMIT_PATH Is Null", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion


        #region  Member Methods
        public void CommitParsedFile( Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileDic )
        {
            CommitParsedRootFolder( selectedFileDic );

            CopyAndCommitInClientPlanFolder( selectedFileDic );
        }

        private void CommitParsedRootFolder( Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileDic )
        {
            foreach( KeyValuePair< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileInfo in selectedFileDic )
            {
                string parseRootDirectory = selectedFileInfo.Key;

                SvnRootFolderCommit( parseRootDirectory );
            }
        }

        private void CopyAndCommitInClientPlanFolder( Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileDic )
        {
            List<string> filePaths = new List<string>();

            foreach( KeyValuePair< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileInfo in selectedFileDic )
            {
                string parsedFolder = selectedFileInfo.Key + "/" + _parsingFolder;

                string[] files = Directory.GetFiles( parsedFolder );

                filePaths.AddRange( files );
            }

            if( false == Directory.Exists( _fileMoveAndCommitPath ) )
                Directory.CreateDirectory( _fileMoveAndCommitPath );

            // Sync
            while( false == Directory.Exists( _fileMoveAndCommitPath ) ) { } 
           
            // Copy
            foreach( string file in filePaths )
            {
                string copyFileRootPath = _fileMoveAndCommitPath;

                string copyFilePath = copyFileRootPath + "/" + Path.GetFileName( file );
                
                if( false == File.Exists( copyFilePath ) )
                {
                    FileStream fstream = File.Create( copyFilePath );
                    fstream.Dispose();
                }

                // Sync
                while( false == File.Exists( copyFilePath ) ) { } 

                File.Copy( file, copyFilePath, true );
            }

            SvnRootFolderCommit( _fileMoveAndCommitPath );
       }

        private void SvnRootFolderCommit( string rootPath )
        {
            SVNHelper.ExecuteSvnCommand( _svnInfo, "add " + rootPath + " --force" );
            SVNHelper.ExecuteSvnCommand( _svnInfo, "commit " + rootPath + " -m " + _log );
        }
        #endregion
    }
}
